package com.laicunba.ext.activity.act20170703;

import java.math.BigDecimal;
import java.util.Date;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.laicunba.ext.service.SlaveUserService;
import com.laicunba.jms.JmsQueueListener;
import com.laicunba.jms.JmsQueueListenerTemplate;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.ActivityService;
import com.laicunba.service.FinancingTargetBidService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.UserService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

//@Component
public class Act20170703UserRegistListener extends JmsQueueListenerTemplate implements JmsQueueListener {

	@Autowired
	private SlaveUserService slaveUserService;
	@Autowired
	private UserService userService;
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private RegistryService registryService;
	@Autowired
	private FinancingTargetBidService financingTargetBidService;
	@Autowired
	private ActivityService activityService;

	private static int MINITE10 = 600;

	@Override
	public String queue() {
		return "lcb.core.tmp.act20170703regist";
	}

	@Override
	public boolean receive(Message msg) {
		try {
			TextMessage txtMsg = (TextMessage) msg;
			JSONObject msgJson = (JSONObject) new JSONParser().parse(txtMsg.getText());
			// 防重复
			if (CacheUtil.incr("act.20170703." + (String) msgJson.get("id"), MINITE10) > 1) {
				return true;
			}

			String userId = (String) msgJson.get("id");
			String regChanceKey = "act.20170703chance." + userId + ".regist";
			String regChance = CacheUtil.getAttribute(regChanceKey);
			if (StringUtil.isEmpty(regChance)) {
				CacheUtil.setAttribute(regChanceKey, "1");
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
